package com.desktopmng.action;

import java.util.List;

import com.desktopmng.core.support.Pager;
import com.desktopmng.core.utils.Json;


/**
 * 包含页面常用元素的可Json化对象，并封装了一些常用信息的输出方法
 * @author Administrator
 *
 */
@Json
public class BaseJsonAction extends BaseAction implements StanderMsgOutput {

	/**
	 * long
	 */
	private static final long serialVersionUID = -6936401872790301157L;

	@Json
	protected Long page;

	@Json
	protected Long total;

	@Json
	protected List<Row> rows;

	@Json
	protected String flag;

	@Json
	protected Object msg;

	protected String taskId;

	protected String proId;

	/**
	 * @return flag
	 */
	public String getFlag() {
		return flag;
	}

	/**
	 * @param flag
	 *            要设置的 flag
	 */
	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public String getProId() {
		return proId;
	}

	public void setProId(String proId) {
		this.proId = proId;
	}

	/**
	 * @return msg
	 */
	public Object getMsg() {
		return msg;
	}

	/**
	 * @param msg
	 *            要设置的 msg
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * @param page
	 *            要设置的 page
	 */
	public void setPage(Long page) {
		this.page = page;
	}

	/**
	 * @param total
	 *            要设置的 total
	 */
	public void setTotal(Long total) {
		this.total = total;
	}

	/**
	 * @param rows
	 *            要设置的 rows
	 */
	public void setRows(List<Row> rows) {
		this.rows = rows;
	}

	/**
	 * @return page
	 */
	public Long getPage() {
		return page;
	}

	/**
	 * @return total
	 */
	public Long getTotal() {
		return total;
	}

	/**
	 * @return rows
	 */
	public List<Row> getRows() {
		return rows;
	}

	public void WrapQueryPage(List<Row> rows, Pager pager) {
		page = pager.getCurrentPageNo();
		total = pager.getTotalCount();
		this.rows = rows;
	}

	protected void isDuplicated(ActionHandle handle) {
		action("", "获取数据发生异常！", handle);
	}

	protected void saveOrUpdate(ActionHandle handle) throws Exception {
		action("保存成功！", "保存失败！", handle);
		// Struts2Utils.renderHtml2(flag, String.valueOf(msg));
		super.response.flushBuffer();
	}

	protected void del(ActionHandle handle) {
		action("删除成功！", "删除失败！", handle);
	}

	protected void update(ActionHandle handle) throws Exception {
		action("修改成功！", "修改失败！", handle);
	}

	protected void action(String successMsg, String failMsg, ActionHandle handle) {
		flag = "1";
		msg = "";
		try {
			boolean success = handle.action();
			if (success) {
				msg = successMsg;
			} else {
				flag = "0";
				msg = failMsg;
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
			flag = "2";
			msg = failMsg + "原因:" + e.getMessage();
		}
	}

	@Json
	public static class Row {
		@Json
		private Long id;
		@Json
		private Object cell;

		/**
		 * @param id
		 *            要设置的 id
		 */
		public void setId(Long id) {
			this.id = id;
		}

		/**
		 * @return id
		 */
		public Long getId() {
			return id;
		}

		/**
		 * @return cell
		 */
		public Object getCell() {
			return cell;
		}

		/**
		 * @param cell
		 *            要设置的 cell
		 */
		public void setCell(Object cell) {
			this.cell = cell;
		}

	}

	@Json
	public static class CellEntity {
		@Json
		private String value;

		@Json
		private String displayName;

		@Json
		private String url;

		@Json
		private ExtParam[] extParams;

		/**
		 * @return value
		 */
		public String getValue() {
			return value;
		}

		/**
		 * @param value
		 *            要设置的 value
		 */
		public void setValue(String value) {
			this.value = value;
		}

		/**
		 * @return displayName
		 */
		public String getDisplayName() {
			return displayName;
		}

		/**
		 * @param displayName
		 *            要设置的 displayName
		 */
		public void setDisplayName(String displayName) {
			this.displayName = displayName;
		}

		/**
		 * @return url
		 */
		public String getUrl() {
			return url;
		}

		/**
		 * @param url
		 *            要设置的 url
		 */
		public void setUrl(String url) {
			this.url = url;
		}

		/**
		 * @return extParams
		 */
		public ExtParam[] getExtParams() {
			return extParams;
		}

		/**
		 * @param extParams
		 *            要设置的 extParams
		 */
		public void setExtParams(ExtParam[] extParams) {
			this.extParams = extParams;
		}
	}

	@Json
	public static class ExtParam {

		@Json
		private String name;

		@Json
		private String value;

		/**
		 * @return name
		 */
		public String getName() {
			return name;
		}

		/**
		 * @param name
		 *            要设置的 name
		 */
		public void setName(String name) {
			this.name = name;
		}

		/**
		 * @return value
		 */
		public String getValue() {
			return value;
		}

		/**
		 * @param value
		 *            要设置的 value
		 */
		public void setValue(String value) {
			this.value = value;
		}

	}

	public void setMsg(Object msg) {
		this.msg = msg;

	}
}
